<template>
  <el-form-item>
    <el-checkbox v-if="showDelete" true-label="1" false-label="0" v-model="isDel" @change="lookDelChange" border>已删门店</el-checkbox>
    <el-select filterable placeholder="选择门店" v-model="storeId" style="width:300px">
      <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
</template>
<script>
import { getStoreList } from '@/api/common/store.js';
export default {
  name: 'DjStoreSelect',
  props: {
    needItem: {
      type: Boolean,
      default: false
    },
    showDelete: {
      type: Boolean,
      default: true
    },
    category: {
      type: Number,
      required: true
    },
    defaultStore: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      isDel: 0,
      storeId: this.defaultStore,
      storeList: [],
      storeListUndelete: null,
      storeListDelete: null
    };
  },
  watch: {
    defaultStore: function() {
      this.storeId = this.defaultStore;
    },
    storeId: function() {
      this.valueChanged();
    }
  },
  created() {
    this.storeId = this.defaultStore;
    this.getStore();
  },
  methods: {
    valueChanged() {
      if (!this.storeList.length) {
        return;
      }
      let val = {
        look_del: this.isDel,
        store_id: this.storeId
      };
      if (this.needItem) {
        let obj = this.storeList.find(item => {
          return item.id === this.storeId;
        });
        if (obj == undefined) {
          obj = { id: '', name: '' };
        }
        this.$emit('change', val, obj);
      } else {
        this.$emit('change', val);
      }
    },
    lookDelChange() {
      this.getStore();
    },
    // 获取门店
    getStore() {
      if (this.isDel == 1) {
        if (this.storeListDelete && this.storeListDelete.length > 0) {
          this.storeList = this.storeListDelete;
          // this.setSelectStore(this.storeList);
          return;
        }
      } else {
        if (this.storeListUndelete && this.storeListUndelete.length > 0) {
          this.storeList = this.storeListUndelete;
          // this.setSelectStore(this.storeList);
          return;
        }
      }
      getStoreList({
        look_del: this.isDel,
        category_id: this.category
      }).then(response => {
        this.storeList = response;
        let data = response.filter(res => res.id == this.defaultStore);
        if (data.length > 0) {
          this.storeId = data[0].id;
        } else {
          if (response.length > 0) {
            this.storeId = response[0].id;
          }
        }
        if (this.isDel) this.storeListDelete = response;
        else this.storeListUndelete = response;
        this.valueChanged();
        // this.setSelectStore(this.storeList);
      });
    }
    // setSelectStore(val) {
    //   let data = val.filter(res => res.id == this.$store.getters.optionId);
    //   if (data.length > 0) {
    //     this.storeId = data[0].id;
    //   } else if (val && val.length > 0) {
    //     this.storeId = val[0].id;
    //   } else {
    //     this.storeId = "";
    //   }
    //   this.valueChanged();
    // }
  }
};
</script>